package ProjectoX;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public abstract class Personaje {
	
	protected Posicion Pos;
	protected String [] NombresSprite;
	protected int FrameActual;
	protected int frameSpeed;
	protected int t;
	protected Nivel miNivel;
	protected SpriteCache spriteCache;
	protected boolean markedForRemoval;
	protected Movimiento Mov;
	
	public Personaje(Nivel nivel) {
		miNivel = nivel;
		spriteCache = miNivel.getSpriteCache();
		FrameActual = 0;
		frameSpeed = 1;
		t = 0;
		Mov = new Movimiento();
	}
	
	public Rectangle getBounds() {
		return new Rectangle(Pos.getX(),Pos.getY(),Pos.getAncho(),Pos.getAlto());
	}
	
	public Movimiento GetMov(){
		return Mov;
	}

	public abstract void colision(Componente C);
	

	
	public void paint(Graphics2D g){
		if (Pos.getX()>=320)
		g.drawImage( spriteCache.getSprite(NombresSprite[FrameActual]), 320,Pos.getY(), miNivel );
		else
			g.drawImage( spriteCache.getSprite(NombresSprite[FrameActual]), Pos.getX(),Pos.getY(), miNivel );	
	}
	 
	public void setSpriteName(String [] names) { 
		NombresSprite = names;
		FrameActual=0;
		Pos.setAlto(0);
		Pos.setAncho(0);
		for (int i = 0; i<names.length; i++) {
			BufferedImage image = spriteCache.getSprite(NombresSprite[i]);
			Pos.setAlto(Math.max(Pos.getAlto(), image.getHeight()));
			Pos.setAncho(Math.max(Pos.getAncho(), image.getWidth()));
		}
	}  
	
	public void setFrameSpeed(int i) {
		frameSpeed = i; 
	}
	
	public void actuar() {
		t++;
		if (t % frameSpeed ==0) {
			t = 0;
			FrameActual = (FrameActual+1) % NombresSprite.length;
		}
	}
	
	public int getFrameSpeed() {
		return frameSpeed; 
	}
	
	public Posicion getPos() {
		return Pos;
	}
	
	public void remove() {
		markedForRemoval = true;
	}
	
	public boolean isMarkedForRemoval() {
		return markedForRemoval;
	}
	
}